package com.example.tests;

import android.content.Intent;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.Button;
import android.widget.TextView;

import com.example.model.User;
import com.example.model.UserList;
import com.example.views.AdminUserActivity;
import com.example.views.MainActivity;


public class adminTest extends ActivityInstrumentationTestCase2 <AdminUserActivity> {
	
	private AdminUserActivity mActivity;
	private Button createAdmin;
	private Button unlockUser;
	private Button deleteB;
	private TextView userType, locked;
	private UserList uList;
	private User tester;
	

	@SuppressWarnings("deprecation")
	public adminTest() {
	    super("com.android.example.views", AdminUserActivity.class);
	}
	
	/*
	 * (non-Javadoc)
	 * @see android.test.ActivityInstrumentationTestCase2#setUp()
	 * 
	 * This sets up the JUNIT 
	 */
	@Override
	public void setUp() throws Exception{
		setActivityInitialTouchMode(false);
		super.setUp();
		Intent i = createIntent();;
		uList = new UserList();
		tester = uList.getUserByUsername("admin");
		setActivityIntent(i);
		mActivity = getActivity();
		
		createAdmin = (Button) mActivity.findViewById(
				com.example.R.id.button_admin);
		
		unlockUser = (Button) mActivity.findViewById(
				com.example.R.id.button_lock);
		
		deleteB = (Button) mActivity.findViewById(
				com.example.R.id.button_delete);
		
		userType = (TextView) mActivity.findViewById(com.example.R.id.text_usertype);
		locked = (TextView) mActivity.findViewById(com.example.R.id.text_accountstatus);

	}
	
	/*
	 * This is just in initial make sure the page loads fine test, nothing tricky
	 */
	public void testAPreConditions() {
		mActivity.runOnUiThread(new Runnable(){

			@Override
			public void run() {	 
				uList = new UserList();
				tester = uList.getUserByUsername("admin");
				assertTrue(mActivity != null);
				assertEquals(userType.getText(), "Administrator");
				assertEquals(locked.getText(), "Account Unlocked");
				assertEquals(tester.getUserName(), "admin");
				assertEquals(tester.isAdmin(), true);
			}
		}); 
	  }
	
	/*
	 * This tests the button that turns someone into an admin or not. Note this assumes what your testing is originally an admin.
	 */
	public void testBAdminButton() {
		mActivity.runOnUiThread(new Runnable(){

			@Override
			public void run() {	  
				
				uList = new UserList();
				tester = uList.getUserByUsername("admin");
				tester.setIsAdmin(true);
				
				assertEquals(tester.isAdmin(), true);
				
				createAdmin.performClick();
				TextView userType2 = (TextView) mActivity.findViewById(com.example.R.id.text_usertype);
				assertEquals(userType2.getText(), "Standard User");
				assertEquals("Not Equals",tester.isAdmin(), false);
				
				createAdmin.performClick();
				userType2 = (TextView) mActivity.findViewById(com.example.R.id.text_usertype);
				assertEquals(userType2.getText(), userType.getText());
				assertEquals(tester.isAdmin(), true);
			}
		}); 
	}
	
	/*
	 * Tests the lock button, locking and unlocking the user. Note assumes your test user is originally unlocked. 
	 */
	public void testCLockButton() {
		mActivity.runOnUiThread(new Runnable(){

			@Override
			public void run() {
				uList = new UserList();
				tester = uList.getUserByUsername("admin");
				tester.lock(false);
				
				assertEquals(tester.isLocked(), false);
				assertEquals(locked.getText(), "Account Unlocked");
				
				unlockUser.performClick();
				assertEquals(tester.isLocked(), true);
				assertEquals(locked.getText(), "Account Locked");
				
				unlockUser.performClick();
				assertEquals(tester.isLocked(), false);
				assertEquals(locked.getText(), "Account Unlocked");
			}
		}); 
	}
	
	/*
	 * Tests the delete button and make sure the user is removed from the userlist
	 */
	public void testDeleteButton() {
		mActivity.runOnUiThread(new Runnable(){

			@Override
			public void run() {
				uList = new UserList();
				tester = uList.getUserByUsername("admin");
				assertTrue(uList.getUserByUsername("admin") != null);
				
				deleteB.performClick();
				assertTrue(uList.getUserByUsername("admin") == null);
			}
		}); 
	}
	
	/*
	 * Private function that make sure an Intent is created so that the activity wont crash.
	 */
	private Intent createIntent() {
		Intent i = new Intent();
		i.putExtra("USERNAME_TO_ADMIN","admin");
		i.putExtra(MainActivity.USERNAME,"admin");
		return i;
	}
	
}